package com.partvoyage.server.servlet;

import com.google.gdata.client.GoogleService;
import com.google.gdata.client.authn.oauth.GoogleOAuthHelper;
import com.google.gdata.client.authn.oauth.GoogleOAuthParameters;
import com.google.gdata.client.authn.oauth.OAuthException;
import com.google.gdata.client.authn.oauth.OAuthHmacSha1Signer;
import com.google.gdata.client.authn.oauth.OAuthParameters.OAuthType;
import com.google.gdata.client.authn.oauth.OAuthSigner;
import com.partvoyage.server.utils.Configuration;
import com.partvoyage.shared.ClientConstants;
import com.partvoyage.shared.model.AccessToken;

/**
 * http://code.google.com/intl/fr-FR/apis/gdata/docs/auth/oauth.html#
 * OAuthAuthorizeToken
 * 
 * @author ludovic
 * 
 */
public abstract class GAuthentificatedRemoteServiceServlet extends
		SCMRemoteServiceServlet {

	protected final void authentificate(GoogleService service,
			ClientConstants.AppScope scope) throws OAuthException {

		AccessToken accessToken = getAccessTokenFromSession(scope
				.getIdentifier());
		System.out.println("Demande d'accès au service avec le token: "
				+ accessToken.getOAuthToken() + " et le code secret : "
				+ accessToken.getOAuthTokenSecret());
		GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
//		oauthParameters.setOAuthConsumerKey(Configuration.getConsumerToken());
//		oauthParameters.setOAuthConsumerSecret(Configuration
//				.getConsumerTokenSecret());
//		oauthParameters.setOAuthToken("4%2FYdAZg4y7wb5TMGubrhksCjdAyz7u");
//		oauthParameters.setOAuthTokenSecret(accessToken.getOAuthTokenSecret());
//		oauthParameters.setScope(scope.getUrl());
////		oauthParameters.setOAuthType(OAuthType.THREE_LEGGED_OAUTH); 		
//		service.setOAuthCredentials(oauthParameters, new 
//		OAuthHmacSha1Signer()); 
////	    new GoogleOAuthHelper(new 
////	    		OAuthHmacSha1Signer());
		oauthParameters.setOAuthConsumerKey(Configuration.getConsumerToken());
		oauthParameters.setOAuthConsumerSecret(Configuration.getConsumerTokenSecret());
		
		oauthParameters.setOAuthToken(accessToken.getOAuthToken());
		oauthParameters.setOAuthTokenSecret(accessToken.getOAuthTokenSecret());
		
		service.setOAuthCredentials(oauthParameters, new OAuthHmacSha1Signer());
	}

	protected abstract <T extends GoogleService> T getAuthentificatedService()
			throws OAuthException;

}
